package edu.nus.mtech.sem1.ca.bill;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import edu.nus.mtech.sem1.ca.cust.Customer;
import edu.nus.mtech.sem1.ca.plan.Account;
import edu.nus.mtech.sem1.ca.utils.TelephonyConstants;


public class BillReport {
	public Account account;
	public Customer customer;

	public void billToHtml(ReportModel rm) throws IOException,
			IllegalArgumentException, IllegalAccessException {
		File in = new File(System.getProperty("user.dir")+"\\data\\bill - with placeholderhtml.html");
		File out = new File("Bill_" + rm.getCust_Account() +"_" +rm.getMonth_Year() + ".html");
		if (out.exists()) {
			out.delete();
		}
		out.createNewFile();
		try {
			BufferedReader br = new BufferedReader(new FileReader(in));
			BufferedWriter bw = new BufferedWriter(new FileWriter(out));

			String temp = "";
			String res = "";
			while ((temp = br.readLine()) != null) {
				res = res + temp + "\n";
			}
			res = doReplace(res, rm);
			bw.write(res);
			bw.flush();
			bw.close();

			// Using iexplore as default. It should work without the iexplore
			// too.
			Runtime.getRuntime().exec(
					"cmd /c start iexplore \"" + out.getAbsolutePath() + "\"");

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void allBillsToHtml(ArrayList<ReportModel> rms) throws IOException,
			IllegalArgumentException, IllegalAccessException {
		File in = new File(System.getProperty("user.dir")+"\\data\\bill - with placeholderhtml.html");
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String startdate = sdf.format(calendar.getTime());

		File out = new File("bills_" + startdate + ".html");
		BufferedWriter bw = new BufferedWriter(new FileWriter(out));
		if (out.exists()) {
			out.delete();
		}
		out.createNewFile();
		String res = "";
		String resTemp = "";
		BufferedReader br = new BufferedReader(new FileReader(in));
		String temp = "";
		while ((temp = br.readLine()) != null) {
			resTemp = resTemp + temp + "\n";
		}
		for (ReportModel rm : rms) {
			try {
				res = res + resTemp;
				res = doReplace(res, rm);
				br.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		bw.write(res);
		bw.flush();
		bw.close();
		// Using iexplore as default. It should work without the iexplore too.
		Runtime.getRuntime().exec(
				"cmd /c start iexplore \"" + out.getAbsolutePath() + "\"");

	}

	private String doReplace(String str, ReportModel r)
			throws IllegalArgumentException, IllegalAccessException {
		Field[] f = r.getClass().getFields();

		for (int i = 0; i < f.length; i++) {
			String fieldName = f[i].getName().toUpperCase();
			String fieldValue = (String) f[i].get(r);
			if (fieldValue != null) {
				if (str.indexOf(fieldName) != -1) {
					String s = "$" + fieldName;
					str = str.replace(s, fieldValue);
				}
			} else {
				if (str.indexOf(fieldName) != -1) {
					String s = "$" + fieldName;
					str = str.replace(s, "");
				}
			}
		}
		return str;
	}
}
